<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateProductsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        /**
         *   `id` int(11) NOT NULL AUTO_INCREMENT,
            `name` varchar(80) NOT NULL COMMENT '商品名称',
            `price` decimal(6,2) NOT NULL COMMENT '价格,单位：分',
            `stock` int(11) NOT NULL DEFAULT '0' COMMENT '库存量',
            `category_id` int(11) DEFAULT NULL,
            `main_img_url` varchar(255) DEFAULT NULL COMMENT '主图ID号，这是一个反范式设计，有一定的冗余',
            `from` tinyint(4) NOT NULL DEFAULT '1' COMMENT '图片来自 1 本地 ，2公网',
            `summary` varchar(50) DEFAULT NULL COMMENT '摘要',
            `img_id` int(11) DEFAULT NULL COMMENT '图片外键',
         */
        Schema::create('products', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name', 80);
            $table->decimal('price', 6,2);
            $table->integer('stock');
            $table->integer('category_id')->unsigned();
            $table->string('main_img_url', 255);
            $table->tinyInteger('from');
            $table->string('summary', 50);
            $table->integer('img_id')->unsigned();

            $table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
            $table->foreign('img_id')->references('id')->on('images')->onDelete('cascade');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('products');
    }
}
